#include "index.h"
#include "ui_index.h"
#include "widget.h"
#include "about.h"
#include "czsmdialog.h"
#include "QTime"
#include <QStylePainter>
#include <QMouseEvent>
#include <QPainter>
#include <QPropertyAnimation>

index::index(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::index)
{
    ui->setupUi(this);
    setWindowTitle(tr("四格骨牌")) ;
    this->setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::CustomizeWindowHint);
    connect(ui->pushButton ,SIGNAL(clicked()),this,SLOT(on_exitBtn_clicked()));
    playlist->addMedia(QUrl("qrc:/music/cjml.mp3"));//设置音乐路径
    playlist->setPlaybackMode(QMediaPlaylist::Loop);//列表循环
    player->setPlaylist(playlist);//播放列表
    player->setVolume(100);//设置音量
    if(isPlay){
        player->play();
        ui->pushButton_3->setStyleSheet("image: url(:/images/ysq.png);padding:1px;color:#fff;height:20px;width:20px;border:0px;border-radius:5px;");
    }else {
        player->stop();
        ui->pushButton_3->setStyleSheet("image: url(:/images/mute.png);padding:1px;color:#fff;height:20px;width:20px;border:0px;border-radius:5px;");
    }
}

index::~index()
{
    delete ui;
}
// 延时器，等待 msec 秒，用于动画中的等待时间
bool Delay_MSec_index(unsigned int msec)      // 程序等待 msec 毫秒
{
    QTime time;
    time.start();
    while(time.elapsed() < msec)            //等待时间流逝 msec 毫秒
    QCoreApplication::processEvents();   //处理事件
    return true ;
}

void index::on_exitBtn_clicked()
{
    QApplication* app;
    app->quit();
}

void index::on_startBtnindex_clicked()
{
    QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
    animation->setDuration(500);
    animation->setStartValue(1);
    animation->setEndValue(0);
    animation->start();
    Delay_MSec_index(500);
    player->stop();
    this->hide();
    Widget *w=new Widget();
    w->show();
}
void index::paintEvent(QPaintEvent *event){
    QPainter p(this);
    p.drawPixmap(0,0,this->width(),this->height(),QPixmap(":/images/indexbg.jpg"));
}

//重写鼠标按下事件
void index::mousePressEvent(QMouseEvent *event)
{
    mMoveing = true;
    //记录下鼠标相对于窗口的位置
    //event->globalPos()鼠标按下时，鼠标相对于整个屏幕位置
    //pos() this->pos()鼠标按下时，窗口相对于整个屏幕位置
    mMovePosition = event->globalPos() - pos();
    return QWidget::mousePressEvent(event);
}

//重写鼠标移动事件
void index::mouseMoveEvent(QMouseEvent *event)
{
    //(event->buttons() && Qt::LeftButton)按下是左键
    //鼠标移动事件需要移动窗口，窗口移动到哪里呢？就是要获取鼠标移动中，窗口在整个屏幕的坐标，然后move到这个坐标，怎么获取坐标？
    //通过事件event->globalPos()知道鼠标坐标，鼠标坐标减去鼠标相对于窗口位置，就是窗口在整个屏幕的坐标
    if (mMoveing && (event->buttons() && Qt::LeftButton)
        && (event->globalPos()-mMovePosition).manhattanLength() > QApplication::startDragDistance())
    {
        move(event->globalPos()-mMovePosition);
        mMovePosition = event->globalPos() - pos();
    }
    return QWidget::mouseMoveEvent(event);
}
void index::mouseReleaseEvent(QMouseEvent *event)
{
    mMoveing = false;
}
void index::on_pushButton_3_clicked()
{
    if(isPlay){
        player->stop();
        isPlay=false;
        ui->pushButton_3->setStyleSheet("image: url(:/images/mute.png);padding:1px;color:#fff;height:20px;width:20px;border:0px;border-radius:5px;");
    }else {
        player->play();
        isPlay=true;
        ui->pushButton_3->setStyleSheet("image: url(:/images/ysq.png);padding:1px;color:#fff;height:20px;width:20px;border:0px;border-radius:5px;");
    }
}
void index::on_czsmBtn_clicked()
{
    czsmdialog *c=new czsmdialog();
    c->exec();
}

void index::on_aboutBtn_clicked()
{
    about *a=new about();
    a->exec();
}
